package com.vim.modules.web.service;

import com.vim.common.base.CrudServiceImpl;
import com.vim.common.utils.IdGen;
import com.vim.modules.web.dao.ShopUserAddressMapper;
import com.vim.modules.web.model.ShopUserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ShopUserAddressService extends CrudServiceImpl<ShopUserAddress> {

    @Resource
    private ShopUserAddressMapper shopUserAddressMapper;

    @Resource
    private ShopAreaService shopAreaService;

    @Autowired
    public ShopUserAddressService(ShopUserAddressMapper mapper) {
        super(mapper);
    }

    @Override
    public int save(ShopUserAddress entity) {
        entity.setAddressId(IdGen.uuid());
        return super.save(entity);
    }

    /**
     * 查询默认地址
     * @param userId
     */
    public ShopUserAddress findDeafult(String userId) {
        return shopUserAddressMapper.findDeafult(userId);
    }

    /**
     * 查询地址详情
     * @param addressId
     */
    public ShopUserAddress findAddress(String addressId) {
        ShopUserAddress address = find(addressId);
        address.setAddressInfo(shopAreaService.areaInfo(address.getDeliveryAreaId()));
        return address;
    }

    /**
     * 修改地址
     * @param address
     */
    public void editAddress(ShopUserAddress address) {
        address.setDeliveryProvince(shopAreaService.find(address.getDeliveryProvinceId()).getAreaName());
        address.setDeliveryCity(shopAreaService.find(address.getDeliveryCityId()).getAreaName());
        address.setDeliveryArea(shopAreaService.find(address.getDeliveryAreaId()).getAreaName());
        if(address.getAddressId().equals("-1")){
            //新增
            save(address);
        }else{
            //修改
            update(address);
        }
    }
}

